using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using visAd.ExceptionHandler;
using System.Configuration;
using System.Xml;
using System.IO;

namespace visAd.ServerDataAddIn
{
    public partial class AddInSettings : Form
    {
        string mClassName = "AddInSettings";
        public AddInSettings()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                fbBrowseFolder.ShowDialog();
                txtErrorLogPath.Text = fbBrowseFolder.SelectedPath;
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "btnBrowse_Click");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
          
            }
            
        }

        private void AddInSettings_Load(object sender, EventArgs e)
        {
            try
            {
                txtRefreshInterval.Focus();
                if (visAd.ServerDataAddIn.Properties.Settings.Default.TimerIntervalInSeconds.ToString() != null)
                {
                    txtRefreshInterval.Text = visAd.ServerDataAddIn.Properties.Settings.Default.TimerIntervalInSeconds.ToString();
                }

                if (visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath != null)
                {
                    txtErrorLogPath.Text = visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath;
                }

                //string url = "";

                //url = visAdHelper.GetUrl();

                //if (!String.IsNullOrEmpty(url))
                //{
                //    txtWebServiceUrl.Text = url;
                //}

                ////if (visAd.ServerDataAddIn.Properties.Settings.Default.visAd_ServerDataAddIn_SCConnectService_SCConnectService != null)
                //if (ConfigurationManager.AppSettings["visAd_ServerDataAddIn_SCConnectService_SCConnectService"] != null)
                //{
                //    //txtWebServiceUrl.Text = visAd.ServerDataAddIn.Properties.Settings.Default.visAd_ServerDataAddIn_SCConnectService_SCConnectService;
                //    txtWebServiceUrl.Text = ConfigurationManager.AppSettings["visAd_ServerDataAddIn_SCConnectService_SCConnectService"];
                //}
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "AddInSettings_Load");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
          
            }            
        }

        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();            
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "btnCancel_Click");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);
          
            }

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(txtRefreshInterval.Text) > 0 && Convert.ToInt32(txtRefreshInterval.Text) < 2147484)
                {
                    if (txtRefreshInterval.Text != null)
                    {
                        visAd.ServerDataAddIn.Properties.Settings.Default.TimerIntervalInSeconds = Convert.ToInt32(txtRefreshInterval.Text);
                    }
                    if (txtErrorLogPath.Text != null)
                    {
                        visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath = txtErrorLogPath.Text;
                    }
                    
                    visAd.ServerDataAddIn.Properties.Settings.Default.Save();

                    this.Close();
                }
                else
                {
                    MessageBox.Show("Please enter a valid time interval in seconds. Data Refresh Interval should be between 1 to 2147483 seconds",
                        "Filter Wizard",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }

            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "btnOk_Click");
                //throw vaEx;
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            catch (Exception ex)
            {
                vaCustomException vaEx = new vaCustomException(ex, mClassName, "Filter Wizard");
                vaEx.WriteToLog(visAd.ServerDataAddIn.Properties.Settings.Default.ApplicationLogFilePath, true, true);
                MessageBox.Show("An error has occurred. Please refer the log file", "Filter Wizard", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

       

        private void txtRefreshInterval_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (((int)e.KeyChar < 48) || ((int)e.KeyChar > 57))
            {
                if ((int)e.KeyChar != 8)
                {
                    e.Handled = true;
                }

            }
        }


    }
}